﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
//using System.Linq.Dynamic;
using NLite.Collections;
using System.Collections;
using NLite.Dynamic;
using NLite.Binding;
using NLite.Data;

namespace NLite.Domain
{
    ///// <summary>
    ///// 查询模型扩展类
    ///// </summary>
    //public static class QueryModelExtensions
    //{
    //    /// <summary>
    //    /// 
    //    /// </summary>
    //    /// <typeparam name="T"></typeparam>
    //    /// <param name="modelQuery"></param>
    //    /// <param name="model"></param>
    //    /// <returns></returns>
    //    public static IQueryable<T> Where<T>(this IQueryable<T> modelQuery, SearchModel model) where T:class
    //    {
    //        IQueryable<T> res = modelQuery;

    //        if (model.Items.Count > 0)
    //        {
    //            if (model.ConjunctionOp == ConjunctionOperator.Or)
    //            {
    //                foreach (var item in model.Items)
    //                    item.OrGroup = "a";
    //            }
    //             res = modelQuery.Where(new QueryModel { Items = model.Items });
    //        }

    //        if (!string.IsNullOrEmpty(model.SortColumnName))
    //            res = res.OrderBy(model.SortColumnName, model.SortOrder);

    //        return res;
    //    }
    //}

    ///// <summary>
    ///// 查询模型
    ///// </summary>
    //public class SearchModel
    //{
    //    public ConjunctionOperator ConjunctionOp { get; set; }
    //    public List<Filter> Items { get; set; }

    //    public SearchModel()
    //    {
    //        this.Items = new List<Filter>();
    //    }

    //    public bool IsSearch
    //    {
    //        get
    //        {
    //            return this.Items.Count > 0;
    //        }
    //    }

    //    public int Page { get; set; }
    //    public int Rows { get; set; }
    //    public string SortColumnName { get; set; }
    //    public string SortOrder { get; set; }
    //}

   

    //public interface ISearchModelConverter
    //{
    //    SearchModel Convert(string jsonFilters);
    //}

    //public class SearchModelBinder : IModelBinder
    //{
    //    private  ISearchModelConverter Converter;
    //    public SearchModelBinder(ISearchModelConverter converter)
    //    {
    //        Converter = converter;
    //    }

    //    public object BindModel(BindingInfo info, IDictionary<string, object> valueProvider)
    //    {
    //       SearchModel model = new SearchModel();
    //        bool search = false;
    //        object searchStr = null;


    //        if (valueProvider.TryGetValue("_search", out searchStr)
    //            && bool.TryParse(searchStr as string, out search))
    //        {
    //            var filters = valueProvider["filters"] as string;
    //            if (!string.IsNullOrEmpty(filters))
    //                model = Converter.Convert(filters);
    //            else
    //            {
    //                var filter = new Filter();

    //                filter.Field = valueProvider["searchField"] as string;
    //                if (!string.IsNullOrEmpty(filter.Field))
    //                {
    //                    filter.Value = valueProvider["searchString"] as string;
    //                    filter.SetOp(valueProvider["searchOper"] as string);
    //                    model.Items.Save(filter);
    //                }
                    
    //            }
    //        }

    //        model.Page = int.Parse(valueProvider["page"] as string);
    //        model.Rows = int.Parse(valueProvider["rows"] as string);
    //        model.SortColumnName = valueProvider["sidx"] as string; 
    //        model.SortOrder = valueProvider["sord"] as string; 
       
    //        return model;
        
    //    }
    //}
}
